<?php
/*
 * Created on 2011-2-18
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 class YiLook{
 	
 	
 	const SITEURL = "http://www.yi-look.com";
 	
 	
 	public function __construct(){
 		
 		
 	}
 	
 	/**
 	 * 对file_get_content的封装,可能以后加入超时
 	 * @param string
 	 * @return string
 	 * 
 	 */
 	public static   function getfile($url){
 		
		 $content=file_get_contents($url);
 		$content=mb_convert_encoding($content,'utf-8','gbk,gb2312,utf-8');
 		
 		return  	$content;
 	}
 	
 	/**
 	 * 取得文章内容，返回string
 	 * 
 	 * @param string
 	 * @return string
 	 * 
 	 */
 	 public static function getArticle($url,$stripBr=false){
 		
		$content=self::getfile($url);
 		$regex="#class=ART>(.*)<DIV class=FL>#iUs";
 		preg_match($regex,$content,$match);
 		$content=trim($match[1]); 			
 		if (false==$stripBr)
 			return $content;
 		else	 
 			return self::convert2text($content);
 	
 		
 		
 	}
 	
 	/**
 	 * 通过章节url取得文章标题
 	 * 
 	 * @param string
 	 * @return string
 	 * 
 	 */
 	 public static function getTitleByChapter($url){
 		$content=self::getfile($url);
 		$regex="#<TITLE>《(.*)》.*</TITLE>#iUs";
 		preg_match($regex,$content,$match);
 		$title=trim($match[1]);
 		return $title;
 		
 	}
 	
 	
 	
 	
 	/**
 	 * 
 	 * 把内容转换成txt时，去除标签并且把<br>替换成换行
 	 * 
 	 * @param string
 	 * 
 	 * @return string
 	 */
 	
 	 public static  function convert2text($content){
 		
 		$content=strip_tags($content,'<br>');
 		$content=preg_replace("#(<br>)+#i","\n",$content);
 		return $content;
 		
 	}
 	
 	/**
 	 * 取得章节列表
 	 * 
 	 * @param string
 	 * 
 	 * @return array
 	 * 
 	 */
 	 public  static function getChapterList($url,$siteUrl=self::SITEURL){
 		
 		$listContent=self::getfile($url);
 		//去除混淆视听的<!-内容
 		$stripRegx="#<!--.*-->#iUs";
 		$listContent = preg_replace($stripRegx,'',$listContent);
 		$regex = "#<a href='(read_(.*)_.*\.html)' >(.*)</A>#iUs";
 		//$regex="#href=\"({$siteUrl}/read_(.*)_.*\.html)\">(.*)</A>#iUs";
 		
 		//return self::$siteUrl;
 		#$regex="#href=\"http://www.yi-look.com/read_128324_8736.html\">第7节</A>#iUs";
 		
 		preg_match_all($regex,$listContent,$matches,PREG_SET_ORDER );
 		
 		//sort by sortId 
 		foreach ($matches as $key => $row) {
    		$volume[$key]  = $row[2];
    		
		}
		array_multisort($volume, SORT_ASC, $matches);
		
		
		foreach($matches as &$v){
 			
 			$v['sortId']=$v[2];
 			$v['chapter']=$v[3];
 			$v['contentUrl']=self::SITEURL .'/' .$v[1]; //内容Url
 			
 			//remove all the number Indexs
 			
 			for($i=0;$i<4;$i++)
 				array_shift($v);
 			
 		}
 		
 	
 		return $matches;
 		
 	
 	}
 	
 	/**
 	 * 取作品列表
 	 * 
 	 * @param string  作品列表，如首页列表
 	 * @return array 
 	 */
 	 public static function getWorksList($url){
 		
 		$listContent=self::getfile($url);
 		$regex1="#<table(.*)</TABLE>#iUs";
 		
 		
 		preg_match_all($regex1,$listContent,$match);
 		$listContent=$match[0][2];
 		//var_dump($listContent);
 		
 		$regex="#href='(.*art_.*)'.*《(.*)》</A>.*\?key=(.*)'#iUs";
 		preg_match_all($regex,$listContent,$matches,PREG_SET_ORDER);
 		//var_dump($matches);
 		
 		
 		foreach($matches as &$v){
 			
 			$v['title']=$v[2];
 			$v['author']=$v[3];
 			$v['chapterUrl']=self::SITEURL .'/' .$v[1]; //章节url
 			
 			//remove all the number Indexs
 			
 			for($i=0;$i<4;$i++)
 				array_shift($v);
 			
 		}
 		
 		return $matches;
 		
 		
 		
 		
 	}
 	
 /**
  * 从目录页取得分类名及是否仍然连载中
  * 
  * @param string 章节目录Url如http://www.yi-look.com/art_8736_6163.html
  * @return array 小说分类array('category'=>"舞文",'serialize'=>true/false)
  * 
  */
 	public static function getCategoryByChapterUrl($url){
 		$content=self::getfile($url);
 		$regex="#分类:(.*)\[(.*)\] </SPAN>#iUs";
 		preg_match($regex,$content,$matches);
 		//if (empty($matches)) return false;
 		//var_dump($matches);
 		//去除标题空格包含，这个囧，还全半角中英空格
 		$category=str_replace(array(' ','　'),'',$matches[1]);
 		//var_dump($category);
 		$serialize=($matches[2]=='连载中')?true:false;
 		return array('category'=>$category,'end'=>$serialize);
 		
 		
 	}
 	
 	
 	
 	
 }



